<?php
defined('BASEPATH') or exit('No direct script access allowed');

/**
 *
 * :mongodb操作基础类，请不要随意修改，影响很大（可自己写子类进行扩展）。
 * @date:2016-08-10
 *
 * @author :zhangxihang
 */
class MongoModel extends CI_Model
{

    private $mongo;

    protected $db;

    protected $con;

    public function __construct()
    {
        parent::__construct();
        try {
            $this->mongo = new MongoClient(MONGO_URL);
            $this->db = $this->mongo->{MONGO_TOPIC_DATABASE};
        } catch (Exception $e) {
            $error = $e->getMessage();
            log_message('error', $error);
            return false;
        }
    }

    /* 建立集合连接 */
    public function setConnection($collection)
    {
        try {
            $this->con = $this->db->$collection;
        } catch (Exception $e) {
            $error = $e->getMessage();
            log_message('error', $error);
            return false;
        }
    }

    /* 插入 */
    public function insert($value)
    {
        try {
            $result = $this->con->insert($value);
        } catch (Exception $e) {
            $error = $e->getMessage();
            log_message('error', $error);
            return false;
        }
        return $result;
    }

    /* 更新操作 */
    public function update($conditon, $value, $multiple = false)
    {
        $value = array(
            '$set' => $value
        );
        $multiple = array(
            'multiple' => $multiple
        );
        try {
            $result = $this->con->update($conditon, $value, $multiple);
        } catch (Exception $e) {
            $error = $e->getMessage();
            log_message('error', $error);
            return false;
        }
        return $result;
    }

    /* 查询 */
    public function find($condition, $field = array(), $limit = 0, $skip = 0)
    {
        $result = array();
        if ($field) {
            foreach ($field as $v) {
                $temp[$v] = 1;
            }
            $field = $temp;
        }
        $cursor = $this->con->find($condition, $field)
            ->limit($limit)
            ->skip($skip);
        
        while ($document = $cursor->getNext()) {
            $document['_id'] = $document['_id']->{'$id'};
            $result[] = $document;
        }
        
        return $result;
    }

    /* 获取记录数 */
    public function getCount($condition)
    {
        $count = $this->con->count($condition);
        return $count;
    }

    /* 寻找一条数据，返回的是一维数组 */
    public function findOne($condition, $field = array())
    {
        if ($field) {
            foreach ($field as $v) {
                $temp[$v] = 1;
            }
            $field = $temp;
        }
        $result = $this->con->findOne($condition, $field);
        
        return $result;
    }

    public function __destruct()
    {
        if ($this->mongo) {
            $this->mongo->close();
        }
    }
}